package com.herui.sdyu_lib.adapter.recycle_view.wrapper;

import android.view.View;
import android.view.ViewGroup;

import com.herui.sdyu_lib.adapter.recycle_view.WrapperUtils;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


/**
 * Created by zhy on 16/6/23.
 */
public class LoadMoreWrapper<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        View.OnClickListener {
    public static final int ITEM_TYPE_LOAD_MORE = Integer.MAX_VALUE - 2;

    public static final int LOAD_SUCESS = 0;    // 加载成功
    public static final int LOAD_FAIL = 1;  // 加载失败
    public static final int LOAD_ALL = 2;   // 加载成功
    public static final int ISLOAD = 3; // 正在加载

    private RecyclerView.Adapter mInnerAdapter;
    private View mLoadMoreView;
    private int mLoadMoreLayoutId;

    private ViewHolder loadViewHolder;
    private int loadState = LOAD_SUCESS;

    public LoadMoreWrapper(RecyclerView.Adapter adapter) {
        mInnerAdapter = adapter;
    }

    private boolean hasLoadMore() {
        return mLoadMoreView != null || mLoadMoreLayoutId != 0;
    }


    private boolean isShowLoadMore(int position) {
        return hasLoadMore() && (position >= mInnerAdapter.getItemCount());
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowLoadMore(position)) {
            return ITEM_TYPE_LOAD_MORE;
        }
        return mInnerAdapter.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_LOAD_MORE) {
            if (mLoadMoreView != null) {
                loadViewHolder = ViewHolder.createViewHolder(parent.getContext(), mLoadMoreView);
            } else {
                loadViewHolder = ViewHolder.createViewHolder(parent.getContext(), parent,
                        mLoadMoreLayoutId);
            }
            loadViewHolder.getConvertView().setOnClickListener(this);
            loadViewHolder.getConvertView().setVisibility(View.GONE);
            return loadViewHolder;
        }
        return mInnerAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isShowLoadMore(position)) {
            if (mOnLoadMoreListener != null) {
                if (loadState == LOAD_SUCESS)
                    mOnLoadMoreListener.onLoadMoreRequested();
                else if (loadState == LOAD_FAIL)
                    mOnLoadMoreListener.onLoadFail();
                else if (loadState == LOAD_ALL)
                    mOnLoadMoreListener.onLoadAll();
                else if (loadState == ISLOAD)
                    mOnLoadMoreListener.loading();
            }
            return;
        }
        mInnerAdapter.onBindViewHolder(holder, position);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils
                .SpanSizeCallback() {
            @Override
            public int getSpanSize(GridLayoutManager layoutManager, GridLayoutManager
                    .SpanSizeLookup oldLookup, int position) {
                if (isShowLoadMore(position)) {
                    return layoutManager.getSpanCount();
                }
                if (oldLookup != null) {
                    return oldLookup.getSpanSize(position);
                }
                return 1;
            }
        });
    }


    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        mInnerAdapter.onViewAttachedToWindow(holder);

        if (isShowLoadMore(holder.getLayoutPosition())) {
            setFullSpan(holder);
        }
    }

    private void setFullSpan(RecyclerView.ViewHolder holder) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

        if (lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams)
                    lp;

            p.setFullSpan(true);
        }
    }

    @Override
    public int getItemCount() {
        return mInnerAdapter.getItemCount() + (hasLoadMore() ? 1 : 0);
    }

    public ViewHolder getLoadViewHolder() {
        return loadViewHolder;
    }

    public void setLoadState(int loadState) {
        this.loadState = loadState;

        if (mOnLoadMoreListener != null) {
            if (loadState == LOAD_FAIL)
                mOnLoadMoreListener.onLoadFail();
            else if (loadState == LOAD_ALL)
                mOnLoadMoreListener.onLoadAll();
            else if (loadState == ISLOAD)
                mOnLoadMoreListener.loading();
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnLoadMoreListener != null) {
            if (loadState == LOAD_FAIL)
                mOnLoadMoreListener.onLoadMoreRequested();
        }
    }

    public interface OnLoadMoreListener {
        // 开始加载
        void onLoadMoreRequested();

        // 加载失败
        void onLoadFail();

        // 加载全部
        void onLoadAll();

        // 正在加载
        void loading();
    }

    private OnLoadMoreListener mOnLoadMoreListener;

    public LoadMoreWrapper setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        if (loadMoreListener != null) {
            mOnLoadMoreListener = loadMoreListener;
        }
        return this;
    }

    public LoadMoreWrapper setLoadMoreView(View loadMoreView) {
        mLoadMoreView = loadMoreView;
        return this;
    }

    public LoadMoreWrapper setLoadMoreView(int layoutId) {
        mLoadMoreLayoutId = layoutId;
        return this;
    }
}
